package tt.dz.biz.dao.job;

import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.JobLockVO;

@Repository("jobLockDAO")
public class JobLockDAO
  extends BaseDAO
{
  private static final String LOCK_JOB_BY_CODE = " select job_code as jobCode,job_name as jobName ,state_code as stateCode,condition_code as conditionCode,mutex_code  as mutexCode , last_run_time  as lastRunTime  from ms_job_lock where job_code=:jobCode and status=:status for update nowait ";
  private static final String UPDATE_JOB_STATUS_BY_CODE = "update ms_job_lock set status=:status where job_code=:jobCode";
  private static final String LOCK_JOB_BY_CODELIST = " select job_code as jobCode,job_name as jobName ,state_code as stateCode,condition_code as conditionCode,mutex_code  as mutexCode from ms_job_lock  where job_code in  (:jobCodes) and status=:status for update nowait ";
  private static final String UPDATE_JOB_STATUS_BY_CODELIST = "update ms_job_lock set status=:status where job_code in (:jobCodes)";
  private static final String UPDATE_JOB_RUN_TIME_BY_CODE = "update ms_job_lock set last_run_time=CURRENT_TIMESTAMP where job_code=:jobCode";
  private static final String LIST_ALL_JOB = "select  a.job_code,a.job_name,a.status,a.host_name,a.state_code,a.condition_code,a.mutex_code,a.last_run_time  from ms_job_lock as a  ";
  
  public List<JobLockVO> listAllDailyJob()
  {
    StringBuilder sql = new StringBuilder("select  a.job_code,a.job_name,a.status,a.host_name,a.state_code,a.condition_code,a.mutex_code,a.last_run_time  from ms_job_lock as a  ");
    sql.append(" where a.job_type=2 ");
    return queryForList(sql.toString(), null, JobLockVO.class);
  }
  
  public JobLockVO lockJobByCode(JobLockVO jobVO)
  {
    try
    {
      return (JobLockVO)queryForObject(" select job_code as jobCode,job_name as jobName ,state_code as stateCode,condition_code as conditionCode,mutex_code  as mutexCode , last_run_time  as lastRunTime  from ms_job_lock where job_code=:jobCode and status=:status for update nowait ", jobVO, JobLockVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      LogUtil.info("本机未获得jobcode:" + jobVO.getJobCode() + "的运行权限，参数为：" + jobVO);
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("锁job异常，参数为：" + jobVO, e);
    }
    return null;
  }
  
  public int updateJobStatus(JobLockVO jobVO)
  {
    try
    {
      return Integer.valueOf(save("update ms_job_lock set status=:status where job_code=:jobCode", jobVO)).intValue();
    }
    catch (Exception e)
    {
      LogUtil.error("更新job状态异常，参数为：" + jobVO, e);
    }
    return 0;
  }
  
  public List<JobLockVO> lockJobByCodes(Integer status, List jobCodes)
  {
    try
    {
      MapSqlParameterSource parameters = new MapSqlParameterSource();
      parameters.addValue("jobCodes", jobCodes);
      parameters.addValue("status", status);
      return queryForList(" select job_code as jobCode,job_name as jobName ,state_code as stateCode,condition_code as conditionCode,mutex_code  as mutexCode from ms_job_lock  where job_code in  (:jobCodes) and status=:status for update nowait ", parameters, JobLockVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      LogUtil.info("本机未获得jobCodes:" + jobCodes);
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("锁job异常，参数为jobCodes：" + jobCodes + "状态为status：" + status, e);
    }
    return null;
  }
  
  public int updateJobStatus(Integer status, List<String> jobCodes)
  {
    try
    {
      MapSqlParameterSource parameters = new MapSqlParameterSource();
      parameters.addValue("jobCodes", jobCodes);
      parameters.addValue("status", status);
      return save("update ms_job_lock set status=:status where job_code in (:jobCodes)", parameters);
    }
    catch (EmptyResultDataAccessException e)
    {
      LogUtil.info("本机未获得jobCodes:" + jobCodes);
      return 0;
    }
    catch (Exception e)
    {
      LogUtil.error("更新job异常，参数status为：" + status + ",jobCodes为：" + jobCodes, e);
    }
    return 0;
  }
  
  public int updateLastRunTimeByCode(String jobCode)
  {
    try
    {
      JobLockVO vo = new JobLockVO();
      vo.setJobCode(jobCode);
      return Integer.valueOf(save("update ms_job_lock set last_run_time=CURRENT_TIMESTAMP where job_code=:jobCode", vo)).intValue();
    }
    catch (Exception e)
    {
      LogUtil.error("更新job本次运行时间异常，参数jobCode为：" + jobCode, e);
    }
    return 0;
  }
  
  public JobLockVO queryJobByCode(String jobCode)
  {
    try
    {
      StringBuilder sql = new StringBuilder("select  a.job_code,a.job_name,a.status,a.host_name,a.state_code,a.condition_code,a.mutex_code,a.last_run_time  from ms_job_lock as a  ");
      sql.append(" where a.job_code=:jobCode ");
      JobLockVO vo = new JobLockVO();
      vo.setJobCode(jobCode);
      return (JobLockVO)queryForObject(sql.toString(), vo, JobLockVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      LogUtil.error("通过jobCode查询job异常，参数jobCode为:" + jobCode, e);
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("通过jobCode查询job异常，参数jobCode为:" + jobCode, e);
    }
    return null;
  }
}
